﻿using MedicalManagementAPP.AccountModule;
using MedicalManagementAPP.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MedicalManagementAPP.Forms
{
    public partial class PatientForm : Form
    {
        LoginForm loginForm;
        Person user;
        string curRecordSet;
        public PatientForm(LoginForm loginForm, Person person)
        {
            InitializeComponent();
            this.loginForm = loginForm;
            this.user = person;
            this.Text = person.ToString();
            skinEngine1.SkinFile = @"irisskin2\Wave.ssk";
        }

        private void PatientForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            loginForm.Show();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true) { PswTextBox.PasswordChar = '\0'; } else { PswTextBox.PasswordChar = '*'; }
        }

        private void UnlockCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            bool flag = !UnlockCheckBox.Checked;
            if (!flag) { MessageBox.Show("请注意，您在修改后必须点击下方的提交按钮，并且得到成功的提示后才说明更改成功"); }
            groupBox1.Text = flag ? "病人基本信息(锁定)" : "病人基本信息(可编辑)";
            NameTextBox.ReadOnly = flag;
            PswTextBox.ReadOnly = flag;
            PhoneTextBox.ReadOnly = flag;
        }

        private void PatientForm_Load(object sender, EventArgs e)
        {
            RefreshTextBoxes();
            InitRecordsListView();
        }
        private void RefreshTextBoxes()
        {
            DataRow data = AccountManager.GetPatientInfo(user.PId);
            TextBox[] textBoxes = new TextBox[] { IDTextBox, NameTextBox, DateTextBox, PswTextBox, PhoneTextBox };
            for (int i = 0; i < data.ItemArray.Length; i++)
            {
                textBoxes[i].Text = data[i].ToString();
                textBoxes[i].ReadOnly = true;
            }
        }

        private void RevokeButton_Click(object sender, EventArgs e)
        {
            RefreshTextBoxes();
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {
            string id = user.PId;
            string name = NameTextBox.Text;
            string psw = PswTextBox.Text;
            string phone = PhoneTextBox.Text;
            if (id == "" || name == "" || psw == "" || phone == "") { MessageBox.Show("请不要输入空数据");return; }
            if (UnlockCheckBox.Checked == false) { MessageBox.Show("您没有开启编辑模式");return; }

            string sql = "update Patients set PatientName=@name,Password=@psw,Phone=@phone where PatientID=@id";
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@name",name),
                new SqlParameter("@psw",psw),
                new SqlParameter("@phone",phone),
                new SqlParameter("@id",id)
            };
            if (!Connection.SqlNoQuery(sql, parameters)) { MessageBox.Show("修改失败，请勿与他人共用手机号"); } else { MessageBox.Show("修改成功"); }
            RefreshTextBoxes();
        }
        public void InitRecordsListView()
        {
            // 病历号，登记日期，负责医生，是否缴费，病情描述
            RecordsListView.Items.Clear();
            DataTable data = RecordSetManager.GetRecordSetsForPatient(user.PId);
            foreach(DataRow row in data.Rows)
            {
                ListViewItem item = new ListViewItem(row[0].ToString());
                item.SubItems.Add(row[1].ToString());
                item.SubItems.Add(row[2].ToString());
                item.SubItems.Add(row[3].ToString() == "True" ? "已缴费" : "未缴费");
                item.SubItems.Add(row[4].ToString());
                RecordsListView.Items.Add(item);
            }
        }

        private void RefreshMedicineListView()
        {
            // DataTable:m.MedicineID,MName,Price,MUnit,MedicineAmount
            DataTable data = RecordSetManager.GetRecordSetByID(curRecordSet);
            MedicineListView.Items.Clear();
            foreach (DataRow row in data.Rows)
            {
                // ListView:药品名，单价，单位，数量，单个药品总价
                ListViewItem item = new ListViewItem(row[1].ToString());
                item.SubItems.Add(row[2].ToString());
                item.SubItems.Add(row[3].ToString());
                item.SubItems.Add(row[4].ToString());
                double price = double.Parse(row[2].ToString()) * double.Parse(row[4].ToString());
                item.SubItems.Add(price.ToString());
                MedicineListView.Items.Add(item);
            }
        }

        private void RecordsListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (RecordsListView.SelectedItems.Count != 1) { return; }
            curRecordSet = RecordsListView.SelectedItems[0].SubItems[0].Text;
            label6.Text = "所有病历:(选中以查看详情)  当前选中病历号:" + curRecordSet;
            RefreshMedicineListView();
            DescribeRichTextBox.Text = RecordsListView.SelectedItems[0].SubItems[4].Text;

            // 更新总价
            double totalPrice = 0;
            foreach(ListViewItem item in MedicineListView.Items)
            {
                totalPrice += double.Parse(item.SubItems[4].Text);
            }
            PriceLabel.Text = totalPrice.ToString()+" 元";
        }

        private void PayButton_Click(object sender, EventArgs e)
        {
            if (RecordsListView.SelectedItems.Count != 1) {MessageBox.Show("请先选择一个要支付的记录"); return; }
            if (RecordsListView.SelectedItems[0].SubItems[3].Text == "已缴费") { MessageBox.Show("该项目已经缴费了，请勿重复缴费"); return; }
            DialogResult dialog = MessageBox.Show("确认缴费吗？", "提示", MessageBoxButtons.OKCancel);
            if (dialog == DialogResult.Cancel) { return; }
            if (!RecordSetManager.PayRecordSet(curRecordSet)) { MessageBox.Show("支付失败！");return; }
            MessageBox.Show("支付成功");
            InitRecordsListView();
        }

        private void registerButton_Click(object sender, EventArgs e)
        {
            new RegisterRecord(user,this).Show();
        }
    }
}
